package br.com.massai.model.entity;

import java.math.BigDecimal;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Transient;

import br.com.massai.model.entity.pojo.BudgetItem;
import br.com.massai.model.exceptions.ErrorCore;
import br.com.massai.model.exceptions.SystemException;

@Entity
@Table(name = "ENTRY_ITEM")
@NamedQueries({
	@NamedQuery(name = "EntryItem.find", query = "SELECT e FROM EntryItem e"),
	@NamedQuery(name = "EntryItem.duplicate", query = "SELECT COUNT(e) FROM EntryItem e WHERE e.id =:id") //TODO check condition of duplicate
})
public class EntryItem extends GenericEntity implements Cloneable {

	private static final long serialVersionUID = -6204382284720506802L;

	@Id
	@SequenceGenerator(name = "seqEntryItem", sequenceName = "seq_entry_item", allocationSize = 1)
	@GeneratedValue(generator = "seqEntryItem", strategy = GenerationType.SEQUENCE)
	private Long id;

	@Column(name = "QUANTITY", precision = 10, scale = 2, columnDefinition = "NUMERIC(10,2)")
	private BigDecimal quantity;

	@Column(name = "VALUE_PERCENTAGE", precision = 10, scale = 4, columnDefinition = "NUMERIC(10,4)")
	private BigDecimal valuePercentage;

	@Column(name = "UNIT_PRICE", precision = 10, scale = 2, columnDefinition = "NUMERIC(10,2)")
	private BigDecimal unitPrice;

	@Column(name = "VALUE_NET", precision = 10, scale = 2, columnDefinition = "NUMERIC(10,2)")
	private BigDecimal valueNet;

	@Column(name = "OBSERVATION", nullable = true, columnDefinition = "TEXT")
	private String observation;

	@Embedded
	private BudgetItem budgetItem;

	@ManyToOne(optional = false, fetch = FetchType.EAGER, cascade = CascadeType.MERGE)
	@JoinColumn(name = "ENTRY_ID", referencedColumnName = "ID")
	private Entry entry;

	@ManyToOne(optional = false, fetch = FetchType.EAGER)
	@JoinColumn(name = "GROUP_ITEM_ID", referencedColumnName = "ID")
	private GroupItem groupItem;

	@Transient
	private BigDecimal totalValue;
	
	//PROPRIEDADES DA IMPORTACAO - FORTES
	@Column(name = "VALOR_LIQUIDO", precision = 10, scale = 2, columnDefinition = "NUMERIC(10,2)")
	private BigDecimal valorLiquido;

	public EntryItem() {
		this.quantity = BigDecimal.ZERO.setScale(SCALE_VALUE);
		this.valuePercentage = BigDecimal.ZERO.setScale(SCALE_PERCENTAGE);
		this.unitPrice = BigDecimal.ZERO.setScale(SCALE_VALUE);
		this.totalValue = BigDecimal.ZERO.setScale(SCALE_VALUE);
		this.budgetItem = new BudgetItem();
		this.entry = new Entry();
		this.groupItem = new GroupItem();
	}

	@Override
	public boolean equals(Object obj) {
		if(obj == null) return false;
		if(obj instanceof EntryItem)
			if(((EntryItem)obj).getId().equals(this.getId())) return true;

		return false;
	}
	
	public EntryItem getClone() {
		try {
			return (EntryItem) super.clone();

		} catch (CloneNotSupportedException e) {
			throw new SystemException(ErrorCore.ERROR_CLONEABLE);
		}
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public BigDecimal getQuantity() {
		return quantity;
	}

	public void setQuantity(BigDecimal quantity) {
		this.quantity = quantity;
	}

	public BigDecimal getValuePercentage() {
//		if(valuePercentage != null)
//			return valuePercentage.movePointLeft(2);
		return valuePercentage;
	}

	public void setValuePercentage(BigDecimal valuePercentage) {
		this.valuePercentage = valuePercentage;
	}

	public BigDecimal getUnitPrice() {
		return unitPrice;
	}

	public void setUnitPrice(BigDecimal unitPrice) {
		this.unitPrice = unitPrice;
	}

	public String getObservation() {
		return observation;
	}

	public void setObservation(String observation) {
		this.observation = observation;
	}

	public Entry getEntry() {
		return entry;
	}

	public void setEntry(Entry entry) {
		this.entry = entry;
	}

	public GroupItem getGroupItem() {
		return groupItem;
	}

	public void setGroupItem(GroupItem groupItem) {
		this.groupItem = groupItem;
	}

	public BudgetItem getBudgetItem() {
		return budgetItem;
	}

	public void setBudgetItem(BudgetItem budgetItem) {
		this.budgetItem = budgetItem;
	}

	public BigDecimal getTotalValue() {
		return totalValue;
	}

	public void setTotalValue(BigDecimal totalValue) {
		this.totalValue = totalValue;
	}

	public BigDecimal getValueNet() {
		return valueNet;
	}

	public void setValueNet(BigDecimal valueNet) {
		this.valueNet = valueNet;
	}

	public BigDecimal getValorLiquido() {
		return valorLiquido;
	}

	public void setValorLiquido(BigDecimal valorLiquido) {
		this.valorLiquido = valorLiquido;
	}
	
}
